##=============================================================================
## Appendix Figure 1
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse", "janitor", "haven", "igraph", "stringr", "ggmap",
              "ggthemes", "sf")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##----------------
# Wave I displaced
num_col <- function(x){
    any(!is.character(x))
}

dis1 <- raw_dat %>% 
  janitor::clean_names() %>%
  haven::zap_label() %>% 
  mutate_if(is.character, list(~na_if(., ""))) %>% 
  mutate_if(num_col, as.numeric) %>% 
  select(v1, v31, v35txt, oblast_new, v36txt, v29txt, oblast, v30txt) %>% 
  rename(id = v1,
         displaced = v31,
         current_loc = v35txt,
         current_oblast = oblast_new,
         current_dist = v36txt,
         past_loc = v29txt,
         past_oblast = oblast,
         past_dist = v30txt) %>% 
  filter(displaced == 2) %>% 
  select(-displaced) %>% 
  mutate(past_oblast = case_when(past_oblast == 2 ~ "Kyiv City",
                                 past_oblast == 3 ~ "Kyivska",
                                 past_oblast == 4 ~ "Vinnytska",
                                 past_oblast == 5 ~ "Volynska",
                                 past_oblast == 6 ~ "Dnipropetrovska",
                                 past_oblast == 7 ~ "Donetska",
                                 past_oblast == 8 ~ "Zhytomyrska",
                                 past_oblast == 9 ~ "Zakarpatska",
                                 past_oblast == 10 ~ "Zaporizka",
                                 past_oblast == 11 ~ "Ivano-Frankivska",
                                 past_oblast == 12 ~ "Kirovohradska",
                                 past_oblast == 13 ~ "Luhanska",
                                 past_oblast == 14 ~ "Lvivska",
                                 past_oblast == 15 ~ "Mykolayivska",
                                 past_oblast == 16 ~ "Odeska",
                                 past_oblast == 17 ~ "Poltavska",
                                 past_oblast == 18 ~ "Rivnenska",
                                 past_oblast == 19 ~ "Sumska",
                                 past_oblast == 20 ~ "Ternopilska",
                                 past_oblast == 21 ~ "Kharkivska",
                                 past_oblast == 22 ~ "Khersonska",
                                 past_oblast == 23 ~ "Khmelnytska",
                                 past_oblast == 24 ~ "Cherkaska",
                                 past_oblast == 25 ~ "Chernivetska",
                                 past_oblast == 26 ~ "Chernihivska")) %>% 
  mutate(current_oblast = case_when(current_oblast == 2 ~ "Kyiv City",
                                    current_oblast == 3 ~ "Kyivska",
                                    current_oblast == 4 ~ "Vinnytska",
                                    current_oblast == 5 ~ "Volynska",
                                    current_oblast == 6 ~ "Dnipropetrovska",
                                    current_oblast == 7 ~ "Donetska",
                                    current_oblast == 8 ~ "Zhytomyrska",
                                    current_oblast == 9 ~ "Zakarpatska",
                                    current_oblast == 10 ~ "Zaporizka",
                                    current_oblast == 11 ~ "Ivano-Frankivska",
                                    current_oblast == 12 ~ "Kirovohradska",
                                    current_oblast == 13 ~ "Luhanska",
                                    current_oblast == 14 ~ "Lvivska",
                                    current_oblast == 15 ~ "Mykolayivska",
                                    current_oblast == 16 ~ "Odeska",
                                    current_oblast == 17 ~ "Poltavska",
                                    current_oblast == 18 ~ "Rivnenska",
                                    current_oblast == 19 ~ "Sumska",
                                    current_oblast == 20 ~ "Ternopilska",
                                    current_oblast == 21 ~ "Kharkivska",
                                    current_oblast == 22 ~ "Khersonska",
                                    current_oblast == 23 ~ "Khmelnytska",
                                    current_oblast == 24 ~ "Cherkaska",
                                    current_oblast == 25 ~ "Chernivetska",
                                    current_oblast == 26 ~ "Chernihivska")) %>% 
  mutate(current_loc = ifelse(current_loc == "Kamianske" & 
                                current_oblast == "Dnipropetrovska",
                              "Kamianske (Dnipropetrovska)", current_loc)) %>% 
  mutate(current_loc = ifelse(current_loc == "Kamianske" & 
                                current_oblast == "Odeska",
                              "Kamianske (Odeska)", current_loc)) %>% 
  mutate(current_loc = ifelse(current_loc == "Selets" & 
                                current_oblast == "Rivnenska",
                              "Selets (Rivnenska)", current_loc)) %>% 
  mutate(current_loc = ifelse(current_loc == "Selets" & 
                                current_oblast == "Zhytomyrska",
                              "Selets (Zhytomyrska)", current_loc))

# Create a network of respondent IDP flows
# nodes
nodes1 <- dis1 %>% 
  distinct(past_loc, past_oblast) %>% 
  rename(location = past_loc, adm1_en = past_oblast) %>% 
  mutate(status = "from")

nodes2 <- dis1 %>% 
  distinct(current_loc, current_oblast) %>% 
  rename(location = current_loc, adm1_en = current_oblast) %>% 
  mutate(status = "to")

nodes <- bind_rows(nodes1, nodes2) %>%
  distinct(location, adm1_en, status) %>% 
  mutate(status = ifelse(location == "Cherkasy"|
                           location == "Dnipro"|
                           location == "Kharkiv"|
                           location == "Kramatorsk"|
                           location == "Kropyvnytskyi"|
                           location == "Kyiv"|
                           location == "Lutsk"|
                           location == "Nosivka"|
                           location == "Odesa"|
                           location == "Poltava"|
                           location == "Sumy"|
                           location == "Ternopil"|
                           location == "Vinnytsia"|
                           location == "Zaporizhzhia", "both", status)) %>% 
  mutate(status = factor(status)) %>% 
  distinct(location, adm1_en, status)

rm(nodes1, nodes2)

status <- nodes %>% 
  select(location, status)

nodes <- nodes %>% 
  select(location, location, adm1_en)

nodes <- nodes %>% # 266 unique locations from IDPs in Wave I
  left_join(viina_locs, by = c("location", "adm1_en")) %>% 
  mutate(latitude = ifelse(location == "Shyroka Hreblia", 50.076, latitude),
         longitude = ifelse(location == "Shyroka Hreblia", 36.788, longitude)) %>% 
  mutate(latitude = ifelse(location == "Bila Krynytsia", 47.345, latitude),
         longitude = ifelse(location == "Bila Krynytsia", 33.192, longitude)) %>%
  mutate(latitude = ifelse(location == "Sukha Balka", 48.319, latitude),
         longitude = ifelse(location == "Sukha Balka", 37.76, longitude)) %>%
  mutate(latitude = ifelse(location == "Nova Kuban", 47.114, latitude),
         longitude = ifelse(location == "Nova Kuban", 33.347, longitude)) %>%
  mutate(latitude = ifelse(location == "Velykyi Kobeliachok", 49.365, latitude),
         longitude = ifelse(location == "Velykyi Kobeliachok", 34.115, longitude)) %>%
  mutate(latitude = ifelse(location == "Staryi Saltiv", 50.076, latitude),
         longitude = ifelse(location == "Staryi Saltiv", 36.788, longitude)) %>% 
  mutate(latitude = ifelse(location == "Kamianske (Dnipropetrovska)", 47.618, 
                           latitude),
         longitude = ifelse(location == "Kamianske (Dnipropetrovska)", 34.428,
                            longitude)) %>% 
  mutate(latitude = ifelse(location == "Kamianske (Odeska)", 45.819, 
                           latitude),
         longitude = ifelse(location == "Kamianske (Odeska)", 29.258,
                            longitude)) %>% 
  mutate(latitude = ifelse(location == "Selets (Rivnenska)", 51.623, 
                           latitude),
         longitude = ifelse(location == "Selets (Rivnenska)",  26.600,
                            longitude)) %>% 
  mutate(latitude = ifelse(location == "Selets (Zhytomyrska)", 50.605, 
                           latitude),
         longitude = ifelse(location == "Selets (Zhytomyrska)",  28.765, 
                            longitude)) %>% 
  distinct(location, adm1_en, .keep_all = TRUE) 

nodes2 <- nodes %>%  # 55 missing coords
  filter(is.na(latitude)) %>% 
  select(location, adm1_en) %>% 
  left_join(viina_locs, by = c("location", "adm1_en")) %>% 
  distinct(location, adm1_en, .keep_all = TRUE) %>% 
  select(location, adm1_en, latitude, longitude)

nodes <- nodes %>% 
  drop_na(latitude)

nodes <- nodes %>% 
  bind_rows(nodes2) 

rm(nodes2)

nodes <- nodes %>% 
  left_join(status, by = "location")

## links
noise <- rnorm(n = 275, mean = 0, sd = 0.001)

links <- dis1 %>% 
  select(past_loc, current_loc) %>% 
  rename(from = past_loc, to = current_loc) %>% 
  inner_join(nodes %>% select(location, longitude, latitude), 
             by = c('from' = 'location')) %>%
  rename(x = longitude, y = latitude) %>%
  inner_join(nodes %>% select(location, longitude, latitude), 
             by = c('to' = 'location')) %>%
  rename(xend = longitude, 
         yend = latitude) %>% 
  mutate(xend = xend + noise,
         yend = yend + noise) 

## build network
# ukr_map <- get_googlemap(center = c(lon = 31.858, lat = 49.580),
#                          zoom = 5, maptype = "roadmap",
#                          style = c(feature = "all", 
#                                    element = "labels",
#                                    visibility = "off"), 
#                          color = "bw")

# Plot Network on Map
ggmap(ukr_map) +
  geom_point(data = nodes, 
             aes(x = longitude, y = latitude, color = status), alpha = 0.75) +
  scale_color_manual(values = c("blue", "red", "darkgreen")) +
  geom_curve(data = links,
             aes(x = x, y = y, xend = xend, yend = yend),    
             arrow = arrow(angle = 15, ends = "last", length = unit(0.15, "cm"), 
                           type = "closed"),
             color = "black", alpha = 0.25) +
  coord_quickmap() +
  labs(color = "Migration Status") +
  theme_map() +
  theme(legend.position = "bottom",
        legend.text = element_text(size = 24, family = "Arial Narrow"),
        legend.title = element_text(size = 24, family = "Arial Narrow")) 

## Network plot
# net <- graph_from_data_frame(d = links, vertices = nodes, directed = TRUE) 
# net <- simplify(net, remove.multiple = FALSE, remove.loops = TRUE) 
# 
# l <- layout_with_fr(net)
# 
# plot(net,
#      layout = l, 
#      edge.arrow.size = 0.01, 
#      edge.curved = .1,
#      vertex.size = 0,
#      vertex.label.cex =  0.75)

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================